<template>
  <a-modal
    :title="title"
    :width="950"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    okText="保存并安排任务"
    cancelText="关闭"
  >
    <template v-if="disableSubmit" slot="footer">
      <a-button key="back" @click="handleCancel">关闭</a-button>
    </template>
    <el-form :inline="true" :model="form" size="small">
      <a-row>
        <a-col :span="12">
          <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="任务类名" hasFeedback>
            <a-input
              :disabled="disableSubmit"
              placeholder="请输入任务类名"
              v-decorator="['jobName', { rules: [{ required: true, message: '请输入任务类名!' }] }]"
              allowClear
            />
          </a-form-item>
          <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="任务组名" hasFeedback>
            <a-input
              :disabled="disableSubmit"
              placeholder="请输入任务组名"
              v-decorator="['jobGroup', { rules: [{ required: true, message: '请输入任务组名!' }] }]"
              allowClear
            />
          </a-form-item>
          <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="组内执行顺序" hasFeedback>
            <a-input
              :disabled="disableSubmit"
              placeholder="请输入组内执行顺序(1-9)"
              style="width:100%"
              v-decorator="['jobOrder', { rules: [{ required: true, validator: validateNumber }] }]"
              allowClear
            />
          </a-form-item>
          <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="类型" hasFeedback>
            <a-select
              :disabled="disableSubmit"
              v-decorator="['jobType', { rules: [{ required: true, message: '请选择类型!' }] }]"
              placeholder="请选择类型"
              allowClear
            >
              <a-select-option v-for="item in jobTypeList" :key="item.value">
                {{ item.name }}
              </a-select-option>
            </a-select>
          </a-form-item>
          <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="调用地址" hasFeedback>
            <a-input
              :disabled="disableSubmit"
              placeholder="请输入调用地址"
              v-decorator="['executePath', { rules: [{ required: false, message: '请输入调用地址!' }] }]"
              allowClear
            />
          </a-form-item>
          <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="类完整路径" hasFeedback>
            <a-input
              :disabled="disableSubmit"
              placeholder="请输入类完整路径"
              v-decorator="['className', { rules: [{ required: false, message: '请输入类完整路径!' }] }]"
              allowClear
            />
          </a-form-item>
          <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="任务方法" hasFeedback>
            <a-input
              :disabled="disableSubmit"
              placeholder="请输入任务方法"
              v-decorator="['methodName', { rules: [{ required: true, message: '请输入任务方法!' }] }]"
              allowClear
            />
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="参数值" hasFeedback>
            <a-input
              :disabled="disableSubmit"
              placeholder="请输入参数值"
              v-decorator="['methodParamsValue', { rules: [{ required: false, message: '请输入参数值!' }] }]"
              allowClear
            />
          </a-form-item>
          <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="cron表达式">
            <a-input
              :disabled="disableSubmit"
              placeholder="请输入cron表达式"
              v-decorator="[
                'cronExpression',
                {
                  initialValue: '0/1 * * * * ?',
                  rules: [{ required: true, message: '请输入cron表达式!' }]
                }
              ]"
              allowClear
            />
          </a-form-item>
          <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="错失执行策略" hasFeedback>
            <a-select
              :disabled="disableSubmit"
              v-decorator="['misfirePolicy', { rules: [{ required: true, message: '请选择错失执行策略!' }] }]"
              placeholder="请选择错失执行策略"
              allowClear
            >
              <a-select-option v-for="item in misfirePolicyList" :key="item.value">
                {{ item.name }}
              </a-select-option>
            </a-select>
          </a-form-item>
          <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="租户任务类型" hasFeedback>
            <a-select
              allowClear
              :disabled="disableSubmit"
              v-decorator="[
                'jobTenantType',
                { initialValue: '2' },
                { rules: [{ required: true, message: '请选择租户任务类型!' }] }
              ]"
              placeholder="请选择租户任务类型"
            >
              <a-select-option v-for="item in jobTenantTypeList" :key="item.value" :value="item.value">
                {{ item.name }}
              </a-select-option>
            </a-select>
          </a-form-item>
          <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="租户id" hasFeedback>
            <a-input
              placeholder="请输入租户id"
              :disabled="disableSubmit"
              v-decorator="['tenantId', { initialValue: 1 }, { rules: [{ required: true, message: '请输入租户id!' }] }]"
              allowClear
            />
          </a-form-item>
          <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="备注信息" hasFeedback>
            <a-input
              :disabled="disableSubmit"
              placeholder="请输入备注信息"
              v-decorator="['remark', { rules: [{ required: false, message: '请输入备注信息!' }] }]"
              allowClear
            />
          </a-form-item>
          <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" hidden>
            <a-input placeholder v-decorator="['jobId', validatorRules.jobId]" />
          </a-form-item>
          <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" hidden>
            <a-input placeholder v-decorator="['jobStatus', validatorRules.jobStatus]" />
          </a-form-item>
          <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" hidden>
            <a-input placeholder v-decorator="['jobExecuteStatus', validatorRules.jobExecuteStatus]" />
          </a-form-item>
        </a-col>
      </a-row>
    </el-form>
  </a-modal>
</template>

<script>
import { addSysJob, updateSysJob } from '../js/task'
import pick from 'lodash.pick'

export default {
  name: 'QuartzJobModal',
  data () {
    return {
      title: '操作',
      form: this.$form.createForm(this),
      visible: false,
      model: {},
      labelCol: {
        xs: { span: 24 },
        sm: { span: 7 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 15 }
      },
      confirmLoading: false,
      validatorRules: {},
      isAdd: false,
      disableSubmit: false,
      disable: true,
      // 1、java类;2、spring bean名称;3、rest调用;4、jar调用;9其他
      jobTypeList: [
        { name: 'java类', value: '1' },
        { name: 'spring bean名称', value: '2' },
        { name: 'rest调用', value: '3' },
        { name: 'jar调用', value: '4' },
        { name: '其他', value: '9' }
      ],
      misfirePolicyList: [
        { name: '错失周期立即执行', value: '1' },
        { name: '错失周期执行一次', value: '2' },
        { name: '下周期执行', value: '3' }
      ],
      jobTenantTypeList: [
        { name: '多租户任务', value: '1' },
        { name: '非多租户任务', value: '2' }
      ]
    }
  },
  created () {},
  methods: {
    edit (record) {
      const that = this
      this.model = Object.assign({}, record)
      this.visible = true
      if (this.isAdd) {
        this.$nextTick(() => {
          that.form.resetFields()
        })
      } else {
        this.$nextTick(() => {
          this.form.setFieldsValue(
            pick(
              this.model,
              'jobId',
              'jobName',
              'jobGroup',
              'jobOrder',
              'jobType',
              'executePath',
              'className',
              'methodName',
              'methodParamsValue',
              'cronExpression',
              'misfirePolicy',
              'jobTenantType',
              'jobStatus',
              'jobExecuteStatus',
              'tenantId',
              'remark'
            )
          )
        })
      }
    },

    // 关闭
    close () {
      this.$emit('close')
      this.disableSubmit = false
      this.visible = false
    },

    handleOk () {
      const that = this
      this.form.validateFields((err, values) => {
        if (!err) {
          const params = {
            jobName: values.jobName,
            jobGroup: values.jobGroup,
            jobOrder: values.jobOrder,
            jobType: values.jobType,
            executePath: values.executePath,
            className: values.className,
            methodName: values.methodName,
            methodParamsValue: values.methodParamsValue,
            cronExpression: values.cronExpression,
            misfirePolicy: values.misfirePolicy,
            jobTenantType: values.jobTenantType,
            tenantId: values.tenantId,
            remark: values.remark
          }
          if (this.isAdd) {
            params.jobId = ''
            // JOB状态：1已发布, 2运行中, 3暂停, 4删除
            params.jobStatus = '1'
            params.jobExecuteStatus = '0'

            addSysJob(params)
              .then(res => {
                if (res.code === 0) {
                  that.$message.success('新增成功')
                  that.$emit('ok')
                } else {
                  that.$message.warning(res.msg)
                }
                this.visible = false
              })
              .finally(() => {
                that.confirmLoading = false
                that.close()
              })
          } else {
            params.jobId = values.jobId
            params.jobStatus = values.jobStatus
            params.jobExecuteStatus = values.jobExecuteStatus

            if (values.jobStatus === '2') {
              this.$message.success('定时任务正在运行，不能修改')
            }

            updateSysJob(params)
              .then(res => {
                if (res.code === 0) {
                  that.$message.success('修改成功')
                  that.$emit('ok')
                } else {
                  that.$message.warning(res.msg)
                }
              })
              .finally(() => {
                that.confirmLoading = false
                that.close()
              })
          }
        }
      })
    },

    handleCancel () {
      this.close()
    },

    // 校验执行顺序
    validateNumber (rule, value, callback) {
      if (!value) {
        callback(new Error('请输入组内执行顺序(1-9)!'))
      } else if (!new RegExp(/^[0-9]*$/).test(value)) {
        callback(new Error('执行顺序只能为纯数字!'))
      } else {
        if (value > 9) {
          callback(new Error('执行顺序不能大于9'))
        } else if (value <= 0) {
          callback(new Error('执行顺序不能小于0'))
        } else {
          callback()
        }
      }
    }
  }
}
</script>
